package com.imau.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imau.bean.Role;
import com.imau.common.entiry.PageQuery;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.common.utils.JSONUtil;
import com.imau.service.RoleService;
import com.imau.service.imp.RoleServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "RoleConterllor", value = "/role/*")
    public class RoleConterllor extends Baseconterllor {

    public RoleConterllor() {

        this.service = new RoleServiceImp();
    }



    /**添加用户*/
    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        //1.获取前端
        BufferedReader reader = request.getReader();

        String jsonObj = reader.readLine();

        //2.转换

        //第一步转成map对象 key = >Java类属性 value=>java属性值
        Map<String,Object> map = JSON.parseObject(jsonObj, Map.class);

        //第二步 map转成对应实体类

        Role role = BeanUtil.mapToBean(map,Role.class,true);


        //第三步 获取角色的主键集合
        List roleIds = (List)map.get("roleIds");


        //3.判断用户名是否存在



        //3.调用服务

        boolean b =  ((RoleService) service).saveEntity(role);


        //4.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        boolean b = service.removeWithById(id);

        //3.响应数据


        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));

    }


    public void update (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {

        //1.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();

        //2.转换

        //第一步转成map对象 key = >Java类属性 value=>java属性值
        Map<String,Object> map = JSON.parseObject(jsonStr, Map.class);

        //第二步 map转成对应实体类

        Role role = BeanUtil.mapToBean(map,Role.class,true);


        //第三步 获取角色的主键集合
        List roleIds = (List)map.get("roleIds");


        //3.调用服务

        boolean b = service.updateByEntity(role);


        //4.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));


    }


    public void getByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {


        //1.获取前台分页数据
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize =  Integer.valueOf(request.getParameter("pageSize"));

        //2.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();


        //3.转成对象

        Role role = JSONUtil.jsonToEntity(jsonStr,Role.class);


        //4.调用service方法添加

        Page<Role> page = service.queryPageList(role,new PageQuery(pageNum,pageSize));

        //3.响应数据

        this.responseBody(response,page==null?false:true,new ResultEntity(ResultCode.SUCCESS,page));


    }


    public void getInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        Role role =((RoleService)service).queryById(id);

        //3.响应数据


        this.responseBody(response,role!=null?true:false,new ResultEntity(ResultCode.SUCCESS,role));

    }
}
